// 1.导入http模块
const http = require('http');
const fs = require('fs');
const path = require('path');
//2.创建服务对象
const server = http.createServer((request, response) => {
        let { pathname } = new URL(request.url, 'http://192.168.101.159');
        let root = path.dirname(__dirname) + '/resource';
        //拼接文件路径
        let filePath = root + pathname;
        //读取文件  fs异步API
        fs.readFile(filePath, (err, data) => {
            if (err) {
                response.setHeader('content-Type', 'text/html;charset=utf-8');
                response.statusCode = 500;
                response.end('读取文件失败~~');
                return
            }
            response.end(data);
        })
    })
    // 3.监听端口，启动服务
server.listen(8080, () => {
    console.log('服务已经启动。。。端口8080监听中。。。。');
})